package com.example.androidmainshixun.view;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.androidmainshixun.R;

@SuppressLint("MissingInflatedId")
public class MainActivity extends AppCompatActivity {
    /*private EditText mSearchEdit;*/
    /*private Button mgo_to_sell;*/
    private ImageView mIvCourses;
    private ImageView mIvEdit;
    private ImageView mIvMessage;
    private ImageView mIvMine;
    private ImageView mIvFirst;
    private FirstFragment mFirstFragment;
    private MessageFragment messageFragment;
    private Fragment mContentFragment;
    private View fragmentContainer;

    private FragmentManager mFragmentManager;/*
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private SectionsPagerAdapter sectionsPagerAdapter; // 您的PagerAdapter
*/



    @SuppressLint("WrongViewCast")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mIvFirst=findViewById(R.id.iv_first);
        mIvCourses=findViewById(R.id.iv_courses);
        mIvEdit=findViewById(R.id.iv_edit_publish);
        mIvMessage=findViewById(R.id.iv_message);
        mIvMine=findViewById(R.id.iv_mine);

        mFragmentManager = getSupportFragmentManager();
        showFragment(new MessageFragment());
        showFragment(new FirstFragment());

        fragmentContainer = findViewById(R.id.fragment_container);
/*
        if (savedInstanceState == null) {
            // 首次创建时添加FirstFragment
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, new FirstFragment()) // 确保这里的ID是MainActivity布局中的容器ID
                    .commit();
        }*/


        /*跳转到农技学堂*/
        mIvCourses.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this, AgroSchool.class);
                startActivity(intent);
            }
        });
        /*跳转到发布页*/
        mIvEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this, EditActivity.class);
                startActivity(intent);
            }
        });
        /*显示信息fragment*/
        mIvMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                messageFragment = new MessageFragment();
                showFragment(messageFragment);
            }
        });
        /*显示主页fragment*/
        mIvFirst.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showFragment(new FirstFragment());
            }
        });

        /*跳转到我的*/
        mIvMine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this, MineActivity.class);
                startActivity(intent);
            }
        });

       /* 跳转到搜索
        mSearchEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSearchBoxClick();

            }
        });

        跳转到卖货
        mgo_to_sell.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this, SellerHomepage.class);
                startActivity(intent);
            }
        });*/
        initFragment();
    }


    private void initFragment() {
        mFragmentManager = getSupportFragmentManager();
        mFirstFragment = new FirstFragment();
    }

    public void showFragment(Fragment fragment) {
        if (fragment != null && mContentFragment != fragment) {
            FragmentTransaction transaction=mFragmentManager.beginTransaction();
            if (mContentFragment != null) {
                transaction.hide(mContentFragment);
            }
            transaction.add(R.id.fragment_content,fragment);
            transaction.commit();
            mContentFragment = fragment;
        }
    }

    public void onSearchBoxClick(){
        Intent intent = new Intent(MainActivity.this, SearchActivity.class);
        startActivity(intent);
    }
}